﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LEARNREN.COM.Entity;
using LEARNREN.COM.BLL;
using LEARNREN.COM.TOOL;

namespace LEARNREN.COM.UI.LabelProviders
{
    /// <summary>
    /// 当前栏目属性
    /// </summary>
    class CTPLabel : AbstractLabelProvider
    {
        public CTPLabel(string label)
            : base(label)
        {

        }

        public override string GetHtml()
        {
            try
            {
                string[] paras = LabelProviderHelper.GetProperties(this.LabelString);

                // 设置的参数小于1，返回原文
                if (paras.Length < 1)
                {
                    return this.LabelString;
                }

                Catalogue curc = null;
                if (this.Context.State is Catalogue)
                {
                    curc = this.Context.State as Catalogue;
                }
                else
                {
                    curc = this.GroupLogicFactory.CatalogueLogic.GetByIdFromCache(this.Context.CatalogueID);
                }
                // 如果没有当前的要找的栏目，返回原文
                if (curc == null)
                {
                    return this.LabelString;
                }


                string prop = paras[0];
                switch (prop.ToLower())
                {
                    case "url":
                        return this.GroupLogicFactory.CatalogueLogic.GetCatalogueURL(curc.Id, 1);
                    case "title":
                    case "summary":
                    case "content":
                        string value = typeof(Catalogue).GetProperty(prop).GetValue(curc, null).ToString();
                        if (paras.Length > 1)
                        {
                            int length = 0;
                            if (int.TryParse(paras[1], out length))
                            {
                                return value.ToShortFormat(length);
                            }
                            else
                            {
                                return value;
                            }
                        }
                        else
                        {
                            return value;
                        }
                    default:

                        return typeof(Catalogue).GetProperty(prop).GetValue(curc, null).ToString();
                }
            }
            catch
            {
                return this.LabelString;
            }
        }
    }
}
